<template>
  <el-dialog title="分配角色权限" :visible="showDialogAssign" @close="hClose">
    <el-tree
      ref="refTree"
      :data="permList"
      :props="{ label: 'name' }"
      show-checkbox
      check-strictly
      default-expand-all
      node-key="id"
    />
    <el-button type="primary" @click="hSave">确定</el-button>
    <el-button type="success">取消</el-button>

  </el-dialog>
</template>

<script>
import { getRoleId } from '@/api/rols'
import { getPermissionList, assignPerm } from '@/api/permission'
import { arr2tree } from '@/utils/index'
export default {
  props: {
    showDialogAssign: {
      type: Boolean,
      required: true
    },
    curId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      permList: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async hSave() {
      const keys = this.$refs.refTree.getCheckedKeys()
      console.log(keys)
      const res = await assignPerm({ id: this.curId, permIds: keys })
      console.log(res)
    },
    async getgetRoleIdList() {
      const res = await getRoleId(this.curId)
      console.log(res)
      // 数据回显
      this.$refs.refTree.setCheckedKeys(res.data.permIds)
    },
    async loadPermissionList() {
      const res = await getPermissionList()
      console.log(res)
      const d = arr2tree(res.data, 0)
      this.permList = d
    },
    hClose() {
      console.log('点击了')
      this.$emit('update:showDialogAssign', false)
    }
  }
}
</script>

<style lang="scss">
.el-button {
    margin-top: 10px;
}
</style>
